#include "inputdialog.h"
#include "ui_inputdialog.h"
#include <QIntValidator>
#include <QItemDelegate>
#include <QLineEdit>
#include <QToolTip>

// An validator for user input
class NumberDelegate : public QItemDelegate
{
public:
    NumberDelegate(int limit = 0x3FFF) : top_limit(limit) {}

    QWidget *createEditor(QWidget *parent,
                          const QStyleOptionViewItem &option,
                          const QModelIndex &index) const override
    {
        Q_UNUSED(option);
        Q_UNUSED(index);

        QLineEdit *lineEdit = new QLineEdit(parent);
        // Set validator
        QIntValidator *validator = new QIntValidator(1, this->top_limit, lineEdit);
        lineEdit->setValidator(validator);
        return lineEdit;
    }

private:
    int top_limit;
};

InputDialog::InputDialog(QWidget *parent, int num_of_elem)
    : QDialog(parent), ui(new Ui::InputDialog)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);

    init(num_of_elem);

    connect(this, &QDialog::accepted, this, [this]() { emit deliverArray(getArray()); });
    connect(ui->arrayTable, &QTableWidget::entered, this, &InputDialog::tooltip);
}

InputDialog::~InputDialog()
{
    delete ui;
}

void InputDialog::init(int num_of_elem)
{
    ui->arrayTable->setItemDelegate(new NumberDelegate(num_of_elem));
    ui->arrayTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->arrayTable->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter);
    ui->arrayTable->setMouseTracking(true);

    QFont headerFont;
    headerFont.setBold(true);
    ui->arrayTable->horizontalHeader()->setFont(headerFont);
    ui->arrayTable->verticalHeader()->setFont(headerFont);

    // default row & column count
    row_cnt = 1, column_cnt = 5;

    // adjust row & column number according to num_of_elem
    if (num_of_elem < 10) {
        column_cnt = num_of_elem;
    } else {
        row_cnt = num_of_elem / 5 + (num_of_elem % 5 == 0 ? 0 : 1);
    }

    ui->arrayTable->setColumnCount(column_cnt);
    ui->arrayTable->setRowCount(row_cnt);

    int itemCnt = 0;
    for (int i = 0; i < row_cnt; i++) {
        for (int j = 0; j < column_cnt; j++) {
            if (itemCnt >= num_of_elem) {
                ui->arrayTable->setItem(i, j, new QTableWidgetItem());
                ui->arrayTable->item(i, j)->setFlags(Qt::ItemIsEditable);
            } else {
                ui->arrayTable
                    ->setItem(i, j, new QTableWidgetItem(QString::number(i * column_cnt + j + 1)));
                ui->arrayTable->item(i, j)->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
                itemCnt++;
            }
        }
    }
}

std::vector<size_t> InputDialog::getArray()
{
    std::vector<size_t> arr;
    for (auto i = 0; i < row_cnt; i++) {
        for (auto j = 0; j < column_cnt; j++) {
            arr.push_back(ui->arrayTable->item(i, j)->text().toULongLong());
        }
    }

    return arr;
}

void InputDialog::tooltip(const QModelIndex &index)
{
    QToolTip::showText(QCursor::pos(),
                       QString("第%1个数据，值：%2")
                           .arg(QString::number(index.row() * column_cnt + index.column() + 1),
                                index.data().toString()));
}
